package com.ys.controller.api.business;

import com.ys.config.MenuId;
import com.ys.entity.business.TbPlatformServicePayAccount;
import com.ys.service.business.TbPlatformServicePayAccountService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tbPlatformServicePayAccount")
public class ApiInterTbPlatformServicePayAccountController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTbPlatformServicePayAccountController.class);

    @Autowired
    private CommonApiService<TbPlatformServicePayAccount> commonApiService;

    @Autowired
    private TbPlatformServicePayAccountService tbPlatformServicePayAccountService;

    @RequiresPermissions("tbPlatformServicePayAccount:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TbPlatformServicePayAccount cond){

        cond.setOrderby("A.createTime DESC")
            .addField("B.VGsBm AS vgsbm")
            .addField("B.VGsJc AS vgsjc")
            .addField("B.CUserName AS operatorUserName")
            .addField("C.Symbol AS amazonShopExchangeRate")
            .addField("'PTFW001' AS serviceCode")
            .addField("'透明计划' AS serviceName")
            .addField("(D.UserId + '/' + D.UserName) AS _ys_nameLinked")
            .addLeftJoin("THR_OperateCompanyApply B on B.VGsBm=A.Vgsbm")
            .addLeftJoin("TP_AmazonShopExchangeRate C on C.Id=A.AmazonShopExchangeRateld")
            .addLeftJoin("TS_UserChange D on D.Id=A.CreateChangeld");
        cond = commonApiService.dataAuth(MenuId.TB_PlatformServicePayAccount, cond);

        return commonApiService.searchLay(cond);
    }



    /**
     * 新增
     */
    @RequestMapping("/add")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    		) {
        try {
            logger.info("新增传参：" + params);
            //修改
            return tbPlatformServicePayAccountService.add(params);
        } catch (Exception e) {
            logger.error("新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("修改传参：" + params);
            //修改
            return tbPlatformServicePayAccountService.edit(params);
        } catch (Exception e) {
            logger.error("修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 透明标申报调用
     * @param cond
     * @return
     */
    @RequestMapping("/searchTmb")
    @ResponseBody
    public LayuiTableVoJA searchTmb(@ModelAttribute TbPlatformServicePayAccount cond){

        cond.addField("'PTFW001' AS platformCode");
        cond.addField("'透明计划' AS platformName");
        cond.addLeftJoin("TP_AmazonShopExchangeRate ter ON  ter.id = A.AmazonShopExchangeRateld");//币种管理
        cond.addField("ter.code AS exchangeRateCode");
        cond.addLeftJoin("THR_OperateCompanyApply toca on toca.VGsBm=A.Vgsbm");//运营公司
        cond.addField("toca.VGsJc AS vgsjc");
        cond.where("A.status = 1");
        cond.setOrderby("A.createTime DESC");
        cond = commonApiService.dataAuth(MenuId.THR_TransparencyProgram, cond);
        return commonApiService.searchLay(cond);
    }

}
